/*
 * sample_all.red
 *
 * This file demonstrates the usage of most of Scarlett constructs:
 *
 * fetch from source
 * assignments
 * display variables
 * regular expression types: number, string, date, email
 * strings and integers
 * saving to a file
 * comparisons
 * for every loop
 * method declaration and call
 * insert, remove, join 
 *
 */
 
 
 
 
save '\n' into enter
show enter
 
 
 ///////////////////
 // BEGIN QUERY 1 //
 ///////////////////
 

/* 
* This demonstrates a combination of an assignment statement
* to a variable after retrieving results from a web query :
* assignmentStatement ::= <SAVE> expression <INTO> variable
* fetchExpression ::= <FETCH> regex <FROM> source
*
* Note: uri is one of our regular expression types.
*/
save fetch uri from http://sfbay.craigslist.org/apa into housing


/* Here, we are initializing a counter to 0.
* We can increment the counter by calling: inc | add <var name>
*/
save 0 into numSbayApts


/* Here we have nested if and for statements. We are also demonstrating 
* string regex queries and incrementing the counter as each result is returned. 
* The results are saved into a variable which is dynamically created and the
* text is appended with each iteration.
*/
show 'Here are urls to apartments for rent in the south bay right NOW:'
for every url in housing
  if url contains 'sby' then
    inc numSbayApts
    show url    
  end if
loop

// Displaying query and counter results.
show enter

show 'There are currently :'
show numSbayApts
show 'apartments for rent in the south bay.'
show enter





 ///////////////////
 // BEGIN QUERY 2 //
 ///////////////////


/* This searches bankrate for mortage rates and if the rate is at 3.80% then
* alert the user to buy the home!! This shows that this kind of query can be
* useful for many professions such as finance and real estate.
*/
show 'We are going to check todays mortgage rates to see if it is time to buy the house :'

save fetch number from http://www.bankrate.com/funnel/mortgages/mortgage-results.aspx?market=273&loan=165000&perc=20&prods=1&points= into rates
save 'no' into buy


// We dont yet support floating point numbers so use your imagination 
// and think of this rate as 3.00 %
for every rate in rates
  if rate is same as '3' then
      save '$$$' into buy
  end if
loop

// This is so it's only printed once
if buy is same as 'no' then
  show 'Sorry, you will have to wait.'
else
  show 'BUY HOME NOW!!!'
end if






 ///////////////////
 // BEGIN QUERY 3 //
 ///////////////////

/* Here we demonstrate writing to a file using strings, variables, and number types.
* We also are using a comparison rule: "is same as". This query shows how our language
* writes much like an english sentence!
*/
// Declare some variables
save 'I love summer.' into summer
save 'summer' into timeofyear

write summer into file:///sample_all.txt
write 'I think I will go' into file:///sample_all.txt
if timeofyear is same as 'summer' then
  write 'to the beach!!!' into file:///sample_all.txt
else
  write 'back inside :(' into file:///sample_all.txt
end if

show enter
show 'We just wrote some text to file:///sample_all.txt. Check it out!'
show enter


  



 ///////////////////
 // BEGIN QUERY 4 //
 ///////////////////
 
/* This example code demonstrates declaring and calling methods */

show 'About to declare and call a function, which displays its 3 parameter contents...'
save 'nuts' into can1
save 'bolts' into can2
save 'thingys' into can3

function myFunc
 	param x
 	param y
	param parm3
	
	show 'x is:'
	show x
	
	show 'y is:'
	show y
	
	show 'parm3 is:'
	show parm3
	
end function

myFunc (can1, can2, can3)





 ///////////////////
 // BEGIN QUERY 5 //
 ///////////////////
 
/* 
* This demonstrates our collection type constructs, which are insert, join, and remove.
*
* join is the intersection of two collections
* insert inserts variable contents into a collection
* remove removes variable contents from a collection
*/
show enter
show 'Here, we are demonstrating the insert, join and remove constructs using collections:'

// note: use fetch to give a collection back
//or insert sting 'foo' into foo; foo becomes a collection

insert 'regular' into k1
insert 'expressions' into k1
insert 'are' into k1
insert 'not' into k1
insert 'easy' into k1
insert 'regular' into k2
insert 'expressions' into k2
insert 'are' into k2
insert 'not' into k2
insert 'easy' into k2
insert 'not' into not

join k1 and k2 into k
show k

remove not from k
show k

insert 'thanks to MakPak' into k
show k





 ///////////////////
 // BEGIN QUERY 6 //
 ///////////////////
 
/* Here we demonstrate the usage of our regular expression type: date
* We can fetch a date from a website and see if it has been updated recently to know
* if we should read the new content. The email regular expression type is used
* in the same way, and can be used to find emails on a webpage or some other source. 
*/
show enter
show 'Now we are retrieving dates from a website to see if there has been updated information today:'
save fetch date from http://forums.ddo.com/showthread.php?t=206066 into schedule
show schedule






// END //
show enter
show enter
show enter
show 'done.'
show enter
